package com.tianyang.yukuaifu.business.login.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.jakewharton.rxbinding.view.RxView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseEditActivity;
import com.tianyang.yukuaifu.business.login.contract.LoginConfig;
import com.tianyang.yukuaifu.business.login.contract.SetPasswordContract;
import com.tianyang.yukuaifu.business.login.presenter.SetPasswordPresenter;
import com.tianyang.yukuaifu.module.widget.EditTextWithDel;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Description：忘记密码、修改密码
 */
public class SetPasswordActivity extends BaseEditActivity implements SetPasswordContract.View {

    @BindView(R.id.etPhone)
    EditTextWithDel etPhone;
    @BindView(R.id.etCode)
    EditTextWithDel etCode;
    @BindView(R.id.etPwd)
    EditTextWithDel etPwd;
    @BindView(R.id.etConfirmPwd)
    EditTextWithDel etConfirmPwd;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;

    @BindView(R.id.etPhoneLl)
    LinearLayout etPhoneLl;
    @BindView(R.id.phoneLl)
    LinearLayout phoneLl;
    @BindView(R.id.tvPhone)
    TextView tvPhone;

    private SetPasswordContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_set_password;
    }

    @Override
    protected void initData() {
        super.initData();
        String pwdType = getIntent().getStringExtra(LoginConfig.PWD_TYPE);
        mPresenter = new SetPasswordPresenter(this);
        mPresenter.initType(pwdType);
    }

    @OnClick({R.id.confirmTv})
    public void onClick(View view) {
        if (!isRespondEvent()) return;
        switch (view.getId()) {
            case R.id.confirmTv:
                if (!checkParameter()) return;
                mPresenter.onNextClick(etPhone.getText().toString(), etPwd.getText().toString(),
                        etCode.getText().toString());
                break;
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        RxView.clicks(tvGetCode).throttleFirst(3, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (!isRespondEvent() || !checkParameterForSms()) return;
                mPresenter.sendCode(etPhone.getText().toString());
            }
        });
    }

    @Override
    public void changeByStatus(boolean status) {
        if (status) {
            tvGetCode.setEnabled(false);
            tvGetCode.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
        } else {
            tvGetCode.setText("获取验证码");
            tvGetCode.setEnabled(true);
            tvGetCode.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
        }
    }

    @Override
    public void byoyomi(Long aLong) {
        tvGetCode.setText(aLong + "秒");
    }

    private boolean checkParameterForSms() {
        String userName = etPhone.getText().toString();
        if (TextUtils.isEmpty(userName)) {
            SoftInputUtil.openKeybord(etPhone, mContext);
            ToastUtil.showTextViewPromptShort("手机号码不能为空");
            return false;
        } else if (VerifyUtil.checkPhoneNumber(userName)) {
            SoftInputUtil.openKeybord(etPhone, mContext);
            ToastUtil.showTextViewPromptShort("电话号码输入不正确");
            return false;
        }
        return true;
    }

    /**
     * 校验输入框的值
     */
    public boolean checkParameter() {
        String userName = etPhone.getText().toString();
        String code = etCode.getText().toString();
        String password = etPwd.getText().toString();
        String confirmPwd = etConfirmPwd.getText().toString();
        if (TextUtils.isEmpty(userName)) {
            SoftInputUtil.openKeybord(etPhone, mContext);
            ToastUtil.showTextViewPromptShort("请输入您的手机号");
            return false;
        } else if (VerifyUtil.checkPhoneNumber(userName)) {
            SoftInputUtil.openKeybord(etPhone, mContext);
            ToastUtil.showTextViewPromptShort("电话号码输入不正确");
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            SoftInputUtil.openKeybord(etCode, mContext);
            ToastUtil.showTextViewPromptShort("验证码不能为空");
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            SoftInputUtil.openKeybord(etPwd, mContext);
            ToastUtil.showTextViewPromptShort("密码不能为空");
            return false;
        }
        if (password.length() < 6) {
            SoftInputUtil.openKeybord(etPwd, mContext);
            ToastUtil.showTextViewPrompt("账号密码不能小于6位数");
            return false;
        }
        if (TextUtils.isEmpty(confirmPwd)) {
            SoftInputUtil.openKeybord(etConfirmPwd, mContext);
            ToastUtil.showTextViewPrompt("请再次输入密码");
            return false;
        }
        if (!TextUtils.equals(password, confirmPwd)) {
            SoftInputUtil.openKeybord(etConfirmPwd, mContext);
            ToastUtil.showTextViewPrompt("两次密码输入不一致，请重新输入");
            return false;
        }
        return true;
    }

    @Override
    public void resetPwdView(String pwdType, String accountName) {
        switch (pwdType) {
            case LoginConfig.UPDATE_PWD:
                commonTitle.setTitle("修改密码");
                etPhone.setText(accountName);
                etPhoneLl.setVisibility(View.GONE);
                phoneLl.setVisibility(View.VISIBLE);
                tvPhone.setText(accountName);
                break;
            case LoginConfig.FORGET_PWD:
                commonTitle.setTitle("找回密码");
                break;
            default:
                return;
        }
    }

    @Override
    public void setPresenter(SetPasswordContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
